package br.caixa2.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import br.caixa2.entities.AccountImpl;
import br.caixa2.entities.Client;
import br.caixa2.entities.CurrentAccount;
import br.caixa2.entities.Extract;
import br.caixa2.entities.SavingAccount;
import br.caixa2.enums.AccountType;
import br.caixa2.enums.TypeOperation;

public class AccountDao {

	public static void createAll( List<AccountImpl> accounts ) {
		PrintStream printCurrent = null;
		PrintStream printSaving = null;
		try {
			printCurrent = new PrintStream( new File( "account_current.db" ) );
			printSaving = new PrintStream( new File( "account_saving.db" ) );
			for ( AccountImpl account : accounts ) {
				PrintStream p = account instanceof CurrentAccount ? printCurrent : printSaving;
				p.println( account.getNumber() );
				p.println( account.getBalance() );
				if ( account instanceof CurrentAccount )
					p.println( ( (CurrentAccount) account ).getLimit() );
				else
					p.println( ( (SavingAccount) account ).getVariation() );
				p.println( account.getPassword() );

				Client c = account.getClient();
				p.println( c.getName() + "," + c.getCpf() + "," + c.getPhone() + "," + c.getAdress() + " ," );
				p.println( account.getExtract().size() );
				SimpleDateFormat f = new SimpleDateFormat( "dd/MM/yyyy" );
				for ( Extract e : account.getExtract() )
					p.println( e.getDescription() + "," + e.getValue() + "," + f.format( e.getDate() ) + "," + e.getType().ordinal() );
			}
		} catch ( FileNotFoundException e ) {
			e.printStackTrace();
		} finally {
			if ( printCurrent != null )
				printCurrent.close();
			if ( printSaving != null )
				printSaving.close();
		}
	}

	public static List<AccountImpl> loadAll() {
		List<AccountImpl> accounts = new ArrayList<AccountImpl>();
		try {
			readFile( "account_current.db", accounts, AccountType.CURRENT );
			readFile( "account_saving.db", accounts, AccountType.SAVING );
		} catch ( FileNotFoundException e ) {
			e.printStackTrace();
		}
		return accounts;
	}

	private static void readFile( String fileName, List<AccountImpl> accounts, AccountType type ) throws FileNotFoundException {
		Scanner scan = new Scanner( new File( fileName ) );
		while ( scan.hasNext() ) {
			int number = Integer.parseInt( scan.nextLine() );
			double balance = Double.parseDouble( scan.nextLine() );
			double doublee = Double.parseDouble( scan.nextLine() );
			String password = scan.nextLine();
			String[] str = scan.nextLine().split( "," );
			Client c = new Client( str[0], str[1] );
			c.setPhone( str[2] );
			c.setAdress( str[3] );
			AccountImpl a = null;

			if ( type.equals( AccountType.CURRENT ) )
				a = new CurrentAccount( c, number, doublee );
			else
				a = new SavingAccount( c, number, doublee );

			int i = Integer.parseInt( scan.nextLine() );
			for ( int j = 0; j < i; j++ ) {
				str = scan.nextLine().split( "," );
				String description = str[0];
				double value = Double.parseDouble( str[1] );
				SimpleDateFormat f = new SimpleDateFormat( "dd/MM/yyyy" );
				Date date = null;
				try {
					date = f.parse( str[2] );
				} catch ( ParseException e1 ) {
					e1.printStackTrace();
				}
				a.addExtract( date, description, TypeOperation.values()[Integer.parseInt( str[3] )], value );
			}
			a.setBalance( balance );
			a.setPassword( password );
			accounts.add( a );
		}
	}
}
